package com.example.excelimporter.listener;

import com.example.excelimporter.model.ImportResult;
import com.example.excelimporter.model.TemplateConfig;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.web.client.RestTemplate;

public class HttpPostImportListener implements PostImportListener {
    private final RestTemplate restTemplate;
    private final String url;
    private final String config;

    public HttpPostImportListener(RestTemplate restTemplate, String url, String config) {
        this.restTemplate = restTemplate;
        this.url = url;
        this.config = config;
    }

    @Override
    public void onSuccess(TemplateConfig templateConfig, ImportResult result) {
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        
        WebhookPayload payload = new WebhookPayload();
        payload.templateId = templateConfig.getId();
        payload.templateName = templateConfig.getTemplateName();
        payload.result = result;
        payload.config = config;
        
        HttpEntity<WebhookPayload> request = new HttpEntity<>(payload, headers);
        restTemplate.postForObject(url, request, String.class);
    }

    @Override
    public void onError(TemplateConfig templateConfig, Exception error) {
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        
        WebhookPayload payload = new WebhookPayload();
        payload.templateId = templateConfig.getId();
        payload.templateName = templateConfig.getTemplateName();
        payload.error = error.getMessage();
        payload.config = config;
        
        HttpEntity<WebhookPayload> request = new HttpEntity<>(payload, headers);
        restTemplate.postForObject(url, request, String.class);
    }

    private static class WebhookPayload {
        private String templateId;
        private String templateName;
        private ImportResult result;
        private String error;
        private String config;

        public String getTemplateId() {
            return templateId;
        }

        public void setTemplateId(String templateId) {
            this.templateId = templateId;
        }

        public String getTemplateName() {
            return templateName;
        }

        public void setTemplateName(String templateName) {
            this.templateName = templateName;
        }

        public ImportResult getResult() {
            return result;
        }

        public void setResult(ImportResult result) {
            this.result = result;
        }

        public String getError() {
            return error;
        }

        public void setError(String error) {
            this.error = error;
        }

        public String getConfig() {
            return config;
        }

        public void setConfig(String config) {
            this.config = config;
        }
    }
} 